package com.smart.draw.plantuml.controllers;

import com.smart.draw.plantuml.common.KeyValObj;
import com.smart.draw.plantuml.common.PlantUmlType;
import com.smart.draw.plantuml.request.SubmitRequest;
import com.smart.draw.plantuml.service.PlantUmlTypeService;
import com.smart.draw.plantuml.service.TranscoderService;
import com.smart.draw.plantuml.service.UrlService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xiaoqiang
 * @Version IndexController.java, v 0.1 2025年08月26日 16:52 xiaoqiang
 * @Description: TODO
 */
@Controller
@RequestMapping
public class IndexController {
    // 假设你有一个简单的 Transcoder 服务（实际可用 PlantUML 官方库）
    private final TranscoderService transcoderService = new TranscoderService();

    @Resource
    private UrlService urlService;


    @Resource
    private PlantUmlTypeService plantUmlTypeService;


    @GetMapping("/plantUmlTypes")
    @ResponseBody
    public List<KeyValObj> getPlantUmlTypes() {
        List<KeyValObj> list = new ArrayList<>();
        for (PlantUmlType type : PlantUmlType.values()) {
            list.add(new KeyValObj(type.getType(), type.getDesc()));
        }
        return list;
    }

    // 根据类型返回默认源码和编码结果
    @GetMapping("/default-source")
    @ResponseBody
    public Map<String, String> getDefaultSource(@RequestParam String type) {
        Map<String, String> result = new HashMap<>();
        try {
            PlantUmlType plantUmlType = PlantUmlType.getByType(type);
            if (plantUmlType != null) {
                String source = plantUmlType.getExample();
                result.put("source", source);
                result.put("encodedSource", transcoderService.encode(source));
            } else {
                result.put("error", "Unknown type: " + type);
            }
        } catch (Exception e) {
            result.put("error", "Failed to get default source: " + e.getMessage());
        }
        return result;
    }
    @GetMapping("/defaultExample")
    @ResponseBody
    public ResponseEntity<String> getDefaultExample(@RequestParam("type") String type) {
        PlantUmlType plantUmlType = PlantUmlType.getByType(type);
        return ResponseEntity.ok(plantUmlType.getExample());
    }

    @GetMapping
    public String index(
            HttpServletRequest request,
            @CookieValue(value = "layout", required = false) String layout,
            @RequestParam(value = "type", required = false) String type,
            Model model) {

        PlantUmlType plantUmlType = PlantUmlType.getByType(type);
        String defaultSource = plantUmlType.getExample();




        model.addAttribute("source", defaultSource);
        String encodedSource = transcoderService.encode(defaultSource);
        model.addAttribute("encodedSource", encodedSource);

        String generatedUrl = urlService.generatePngUrl(request,encodedSource);
        model.addAttribute("generatedUrl", generatedUrl);
        model.addAttribute("currentType", plantUmlType.getType());
        model.addAttribute("plantUmlTypes", plantUmlTypeService.getPlantUmlTypes());

        if ("left-right".equals(layout)) {
            return "editor/left-right";
        } else {
            return "editor/fluid";
        }
    }

    @PostMapping("/submit")
    @ResponseBody
    public Map<String, String> submitDiagram(@RequestBody SubmitRequest request) {
        // 这里可以处理保存、生成图片等
        Map<String, String> result = new HashMap<>();
        result.put("status", "success");
        result.put("message", "Diagram received: " + request.getType());
        result.put("preview", transcoderService.encode(request.getSource()));
        return result;
    }
}
